package org.openfidias.jdbase.view;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.controller.AlunoBo;
import org.openfidias.jdbase.lang.Choose;
import org.openfidias.jdbase.model.AlunoDao;

/**
 *
 * @author atila
 */
public class AlunoView {

   private AlunoView() {
   }

   public static AlunoView getInstance() {
      return AlunoViewHolder.INSTANCE;
   }

   private static class AlunoViewHolder {

      private static final AlunoView INSTANCE = new AlunoView();
   }

   public void begin() throws IOException, Exception {
      String option = "";
      while (true) {
         menu();
         System.out.print(Console.bash);
         option = Console.read.nextLine();
         if (Choose.is(".create", option)) {
            // create
            AlunoBo.getInstance().create();
         } else if (Choose.isAny(new String[]{"*", ".read_all"}, option)) {
            AlunoDao.getInstance().list();
         } else if (Choose.is(".read", option)) {
            AlunoBo.getInstance().read();
         } else if (Choose.is(".delete", option)) {
            AlunoBo.getInstance().delete();
         } else if (Choose.is(".quit", option)) {
            // end program.
            System.exit(0);
         } else if (Choose.is(".clear", option)) {
            Console.clearConsole();
         } else if (Choose.is(".back", option)) {
            // leave view.
            break;
         }  else {
            System.out.println("Opção Inválida.");
         }
      }
   }

   public void menu() {
      StringBuilder builder = new StringBuilder();
      builder.append(StringUtils.center(" View Aluno ", 60, " · "));
      builder.append("\n\n");
      builder.append("[.create] Criar Aluno\n");
      builder.append("[.read] Visualizar Aluno\n");
      builder.append("[.read_all][*] Visualizar Todos os Alunos\n");
      builder.append("[.delete] Deletar Aluno\n");
      builder.append("[.quit][.back][.clear]");
      System.out.println(builder.toString());
   }
}
